#!/bin/bash

US="administrador"
DB="administrador"
PA="administrador"
OU="/var/www/linap/mysql"
BI="/usr/bin/mysqldump"

DATE=$(date +"%Y-%m-%d %H:%M:%S")

echo $0 " - # INICIO"
echo $0 " - # Date: " $DATE

dbases=`/usr/bin/mysql --user=$US --password=$PA -e "SHOW DATABASES;" | tr -d "| " | grep -v Database` 
dbases_exclude=(information_schema mysql performance_schema phpmyadmin test)
 
for db in $dbases; do
    if [[ ! ${dbases_exclude[*]} =~ "${db}" ]]
     then
          echo $0 " - # Dumping database: $db"
          FILE=${OU}/export_${db}.sql

          ${BI} --force --opt --user=${US} --password=${PA} --databases ${db} --routines --triggers > ${FILE}
          #gzip ${FILE}
          echo $0 " - # " ${FILE}
          #svn add ${FILE}
    fi
done

echo $0 " - # Export USER PRIVILEGES "

mysql -h localhost -u root -ppassport mysql -Ne "select distinct concat( \"SHOW GRANTS FOR '\",user,\"'@'\",host,\"';\" ) from user;" | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}' | mysql -h localhost -u root -ppassport mysql > ${OU}/export_privileges.sql

# Autocommit svn
#echo $0 " - # Commit"
svn commit /var/www/linap/ -m 'AutoCommit'

echo $0 " - # FIN"
